package com.ruoyi.web.service.impl;

import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.web.domain.GuEveryDay;
import com.ruoyi.web.domain.GuLzg;
import com.ruoyi.web.domain.GuTag;
import com.ruoyi.web.domain.vo.TengXunBanKuaiDataVo;
import com.ruoyi.web.domain.vo.TengXunBanKuaiVo;
import com.ruoyi.web.domain.vo.TengXunGuVo;
import com.ruoyi.web.mapper.GuLzgMapper;
import com.ruoyi.web.mapper.GuTagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.GuStockMapper;
import com.ruoyi.web.domain.GuStock;
import com.ruoyi.web.service.IGuStockService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

/**
 * 股票行业板块Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@Service
public class GuStockServiceImpl implements IGuStockService 
{
    @Autowired
    private GuStockMapper guStockMapper;
    @Autowired
    private GuLzgMapper guLzgMapper;
    @Autowired
    private GuTagMapper guTagMapper;

    /**
     * 查询股票行业板块
     * 
     * @param id 股票行业板块主键
     * @return 股票行业板块
     */
    @Override
    public GuStock selectGuStockById(String id)
    {
        return guStockMapper.selectGuStockById(id);
    }

    /**
     * 查询股票行业板块列表
     * 
     * @param guStock 股票行业板块
     * @return 股票行业板块
     */
    @Override
    public List<GuStock> selectGuStockList(GuStock guStock)
    {
        return guStockMapper.selectGuStockList(guStock);
    }

    /**
     * 新增股票行业板块
     * 
     * @param guStock 股票行业板块
     * @return 结果
     */
    @Override
    public int insertGuStock(GuStock guStock)
    {
        guStock.setCreateTime(DateUtils.getNowDate());
        return guStockMapper.insertGuStock(guStock);
    }

    /**
     * 修改股票行业板块
     * 
     * @param guStock 股票行业板块
     * @return 结果
     */
    @Override
    public int updateGuStock(GuStock guStock)
    {
        guStock.setUpdateTime(DateUtils.getNowDate());
        return guStockMapper.updateGuStock(guStock);
    }

    /**
     * 批量删除股票行业板块
     * 
     * @param ids 需要删除的股票行业板块主键
     * @return 结果
     */
    @Override
    public int deleteGuStockByIds(String[] ids)
    {
        return guStockMapper.deleteGuStockByIds(ids);
    }

    /**
     * 删除股票行业板块信息
     * 
     * @param id 股票行业板块主键
     * @return 结果
     */
    @Override
    public int deleteGuStockById(String id)
    {
        return guStockMapper.deleteGuStockById(id);
    }

    @Override
    public String getDataFromTengXun() {

        if(guStockMapper.selectGuStockIsNullBuyData() > 1){
            return null;
        }


        RestTemplate restTemplate = new RestTemplate();
        //设置Header
        MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
        headers.add("Content-Type", "application/json;charset=UTF-8");
        String url = "https://proxy.finance.qq.com/cgi/cgi-bin/rank/pt/getRank?board_type=hy2&sort_type=price&direct=down&offset=0&count=20";
        //查询当天数据是否入库，如果入库，直接返回

        //发起请求,直接返回对象
        TengXunBanKuaiVo vo = restTemplate.getForObject(url, TengXunBanKuaiVo.class);
        if(vo.getData()!=null){
            int total = Integer.parseInt(vo.getData().getTotal());
            int i = total/100 ;
            int a = total % 100;
            if (a!=0){
                i++;
            }
            for( int b =0 ;b< i;b++){
                String url1 = "https://proxy.finance.qq.com/cgi/cgi-bin/rank/pt/getRank?board_type=hy2&sort_type=price&direct=down&offset="+b*100+"&count=100";
                TengXunBanKuaiVo vo1 = restTemplate.getForObject(url1, TengXunBanKuaiVo.class);
                if(vo1.getData().getRank_list()!=null){
                    for(GuStock guStock : vo1.getData().getRank_list()){
                        guStock.setId(UUID.randomUUID().toString().replace("-",""));
                        guStock.setCreateTime(DateUtils.getNowDate());
                        //存入每日板块
                        guStockMapper.insertGuStock(guStock);
                        //判断 标签数据库是否存在该板块，如果没有，就新增
                        GuTag guTag = new GuTag();
                        guTag.setId(guStock.getCode());
                        guTag.setCreateTime(DateUtils.getNowDate());
                        guTag.setTagName(guStock.getName());
                        guTagMapper.insertGuTag(guTag);
                        //判断 标签数据库是否存在该板块，如果没有，就新增
                        GuLzg lzg = guStock.getLzg();
                        lzg.setId(UUID.randomUUID().toString().replace("-",""));
                        lzg.setCreateTime(DateUtils.getNowDate());
                        //存入每日板块领涨数据
                        guLzgMapper.insertGuLzg(lzg);
                    }
                }

            }

        }
        return "";
    }
}
